package cn.weiron.fun.infrastructure.configuration;


import cn.weiron.fun.infrastructure.AccountAuthorityHandler;
import cn.weiron.fun.infrastructure.AccountLoginRepository;
import cn.weiron.fun.infrastructure.auth.AccountAuthorityVerifier;
import cn.weiron.fun.infrastructure.auth.Authority;
import cn.weiron.fun.infrastructure.auth.BindLoginAccountConverter;
import cn.weiron.fun.infrastructure.utils.SpringContextUtil;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p>基础设施：接口鉴权配置</p>
 *
 * @author 马强
 */
@Configuration
public class AccountAuthorityConfig implements WebMvcConfigurer {
    /**
     * 鉴权参数名
     */
    @Value("${assign-manage.interface.tokenKey}")
    private String tokenKey;

    /**
     * 是否公开
     */
    @Value("${assign-manage.interface.public}")
    private boolean interfacePublic;

    /**
     * 账户参数自动绑定的参数名
     */
    @Value("${assign-manage.interface.loginAccountBindName}")
    private String loginAccountBindName;

    /**
     * 配置鉴权处理器
     */
    @Bean("authorityAccountHandler")
    public Authority.AccountHandler accountHandler(
            @Qualifier("accountLoginCacheRepositoryRedis") AccountLoginRepository loginRepository
    ) {
        AccountAuthorityHandler accountAuthorityHandler = new AccountAuthorityHandler();
        accountAuthorityHandler.setRepository(loginRepository);
        accountAuthorityHandler.setTokenKey(tokenKey);
        return accountAuthorityHandler;
    }

    /**
     * 配置账户参数转换器对象
     */
    @Bean("bindLoginAccountConverter")
    public BindLoginAccountConverter accountConverter(
            @Qualifier("authorityAccountHandler") Authority.AccountHandler accountHandler
    ){
        BindLoginAccountConverter converter=new BindLoginAccountConverter();
        converter.setAccountHandler(accountHandler);
        return converter;
    }

    /**
     * 配置账户转换器
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        BindLoginAccountConverter converter= SpringContextUtil
                .getBean("bindLoginAccountConverter",BindLoginAccountConverter.class);

        registry.addConverter(converter);
    }

    /**
     * 配置鉴权拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        Authority.AccountHandler accountHandler=SpringContextUtil
                .getBean("authorityAccountHandler",Authority.AccountHandler.class);

        AccountAuthorityVerifier verifier=new AccountAuthorityVerifier();
        verifier.setAccountHandler(accountHandler);
        verifier.setPublicRestful(interfacePublic);
        verifier.setLoginAccountBindName(loginAccountBindName);

        registry.addInterceptor(verifier).addPathPatterns("/**");
    }
}